package com.shaoshaohuo.app.ui.fragment;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.PathMeasure;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.Home_Import_Adapter;
import com.shaoshaohuo.app.constant.ShareKey;
import com.shaoshaohuo.app.entity.HomeCommodity;
import com.shaoshaohuo.app.entity.Home_Imported_entity;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.ui.NewDetailsActivity;
import com.shaoshaohuo.app.utils.Okhttputils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 进口
 * Created by Administrator on 2017/6/6.
 */

public class HomeImportationFragment extends Fragment implements AdapterView.OnItemClickListener, Home_Import_Adapter.AdapterListener {
    private String url;
    private View view;
    private ListView home_fragment_listView;
    private List<String> home_fragment_data;//String 改为实体类
    private Home_Import_Adapter home_listView_adapter;
    private HomeCommodity homeCommodity;
    private Home_Imported_entity home_imported_entity;
    private List<Object> list;
    List<Home_Imported_entity.DataBean> importlist;
    private Handler handler = new Handler();
    //    private ViewPager home_fragment_viewPager;
//    private List<ImageView> home_viewPager_picture;
//    private MyViewPagerAdapter viewPagerAdapter;
    private int picture_index = 0;
    private int[] picture = new int[]{R.mipmap.backgroud, R.mipmap.logo, R.mipmap.gouwuche};
    private RelativeLayout home_relativeLayout;
    private SwipeRefreshLayout home_hot_swip;

    public HomeImportationFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.home_fragment_page, container, false);
        url = getArguments().getString("url");
//        Toast.makeText(getActivity(), url, Toast.LENGTH_SHORT).show();
        initView();
        RequestData();
//        decideBanner();
//        getData();//假数据
        bindAdapter();
//        initTimer();
        initListener();
        return view;
    }

    private void initListener() {
        home_fragment_listView.setOnItemClickListener(this);
        home_listView_adapter.setHomeAdapterListener(this);
        home_hot_swip.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                RequestData();
                home_hot_swip.setRefreshing(false);
                Log.e("TAG", "刷新");
            }
        });
    }

//    private void initTimer() {
//        new Timer().schedule(new TimerTask() {
//            @Override
//            public void run() {
//
//                if (picture_index < picture.length) {
//                    picture_index++;
//                } else {
//                    picture_index = 0;
//                }
//                home_fragment_viewPager.setCurrentItem(picture_index);
//            }
//        }, 1000, 1000);
//    }

//    private void decideBanner() {
//        if (url.equals(HttpConfig.SubUrl.HOT_SALE)) {
//            home_fragment_viewPager.setVisibility(View.VISIBLE);
//        } else {
//            home_fragment_viewPager.setVisibility(View.GONE);
//        }
//    }

//    private void getData() {
//        ImageView imageView;
//        for (int i = 0; i < 10; i++) {
//            home_fragment_data.add(i + "元/斤");
//        }
//        for (int j = 0; j < picture.length; j++) {
//            imageView = new ImageView(getActivity());
//            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//            imageView.setImageResource(picture[j]);
//            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//            home_viewPager_picture.add(imageView);
//        }

//    }

    //网络请求
    private void RequestData() {
//        Map<String, String> map = RequestParam.getHeaderParams();
//        map.put("userid", "32237");
//        map.put("usertoken", "d99duJgC6uyZsqvLK4abD3H%2BKqHIcni%2FylO9Hn5kBE7AhJ2goQg6a4aRpm%2BNCWwgie3rmalwygxPpxacX3BL88VD0DADuC2IXJLGPkQ7vIy%2BVRkaglXmMf6CLZ2JXfYuSPc2AEDnuNPF0BVanvbMb8kLKX4yBQT%2Fh6ZtF4ocqEhbdCEDnBoHp3HvyTNomYPurxT8ccs6T2aZZe84wshnkcFTw6hCfR%2BUzE0xMHMd7uggt7XB%2FUuezUYW7f1ETD6K4aDjqvBCMgDzW53X%2BMIw3xbEXvfEc%2FwWe54EowS2PmkKXtvVEWxvkvrboBSf5Q%2F8y2AQFeSbmrkzcYKtdwiqy4CucpTh%2F5D2PehpyMssJVq5JPQzU4MrWxsSf0%2B%2FkGcT3J4atV2bkpFrAq1%2B");
//        Okhttputils.Instanse(getActivity()).getnet(map, getActivity(), HttpConfig.BASE_HOST_URL + url, new Okhttputils.OkutListener() {
//            @Override
//            public void error(Call call, IOException e) {
//
//            }
//
//            @Override
//            public void error(Call call, int code) {
//
//            }
//
//            @Override
//            public void suseff(Call call, Response response, Object o) {
//                home_imported_entity = (Home_Imported_entity) o;
//                importlist = home_imported_entity.getData().getImportlist();
//                list.addAll(home_imported_entity.getData().getImportlist());
////                home_listView_adapter.notifyDataSetChanged();
//                handler.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        home_listView_adapter.setdate(importlist);
//                    }
//                });
//            }
//        }, Home_Imported_entity.class);


        ShopHttpConfig.ecmain(getContext(), url, new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {

            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                home_imported_entity = (Home_Imported_entity) o;
                importlist = home_imported_entity.getData();
                list.addAll(home_imported_entity.getData());
//                home_listView_adapter.notifyDataSetChanged();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        home_listView_adapter.setdate(importlist);
                    }
                });
            }
        }, Home_Imported_entity.class);
    }

    private void bindAdapter() {
        home_fragment_listView.setAdapter(home_listView_adapter);
//        home_fragment_viewPager.setAdapter(viewPagerAdapter);
    }

    private void initView() {
        home_fragment_listView = (ListView) view.findViewById(R.id.home_fragment_listView);
        home_fragment_data = new ArrayList<>();
        list = new ArrayList<>();
        importlist = new ArrayList<>();
        home_listView_adapter = new Home_Import_Adapter(getActivity(), importlist);
//        home_fragment_viewPager = (ViewPager) view.findViewById(R.id.home_fragment_viewPager);
//        home_viewPager_picture = new ArrayList<>();
//        viewPagerAdapter = new MyViewPagerAdapter(getActivity(), home_viewPager_picture);
        home_relativeLayout = (RelativeLayout) view.findViewById(R.id.home_relativeLayout);
        home_hot_swip = (SwipeRefreshLayout) view.findViewById(R.id.home_hot_swip);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Toast.makeText(getActivity(), "跳转", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getContext(), NewDetailsActivity.class);
        String catid = importlist.get(position).getCatid();
        intent.putExtra(ShareKey.CATID, catid);
        intent.putExtra("imgurl", importlist.get(position).getImg());
        startActivity(intent);
    }

    private ValueAnimator valueAnimator;
    private float[] pos = new float[2];
    private float[] tan = new float[2];

    @Override
    public void pathmove(final PathMeasure pathMeasure, float x, float y) {
        final ImageView view = new ImageView(getContext());
        view.setImageResource(R.mipmap.round_red);
        view.setLayoutParams(new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//        conten.addView(view, conten.getChildCount() - 1);
        home_relativeLayout.addView(view);
        valueAnimator = ValueAnimator.ofFloat(0, pathMeasure.getLength());
        float length = pathMeasure.getLength();
        Log.e("TAG", "pathmove: " + length);
        long duration = (long) ((long) length > 200 ? length -= 200 : length);
        valueAnimator.setDuration(duration);
        valueAnimator.setRepeatCount(0);
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Float animatedValue = (Float) animation.getAnimatedValue();
                pathMeasure.getPosTan(animatedValue, pos, tan);
                float x = pos[0];
                float y = pos[1];
                view.setTranslationX(x);
                view.setTranslationY(y);
                Log.d("TAG", "onAnimationUpdate: " + x + "y" + y + "float" + animatedValue);
//                Newclassificationshaopdataadapter.this.view.layout((int) x, (int) y, Newclassificationshaopdataadapter.this.view.getWidth() + (int) x, Newclassificationshaopdataadapter.this.view.getHeight() + (int) y);
//                Newclassificationshaopdataadapter.this.view.setTranslationX(x);
//                Newclassificationshaopdataadapter.this.view.setTranslationY(y);

            }
        });
        valueAnimator.start();
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                home_relativeLayout.removeView(view);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                home_relativeLayout.removeView(view);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

    }
}
